package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.ReqSkuDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: SGS
 * @Date : 2020/11/17 16:44
 * @Version :
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Override
    public void updateStockById(HashMap<Long,Integer> map) {
        Set<Map.Entry<Long, Integer>> entries = map.entrySet();
        for (Map.Entry<Long, Integer> json : entries) {
            Long id = json.getKey();
            Integer updateStock = json.getValue();
            Sku sku = getById(id);
            Integer stock = sku.getStock();
            if (updateStock>stock){
                throw new LyException(500,"数值大于库存容量");
            }
            update().eq("id",id).set("stock",stock-updateStock).update();

        }
    }

}
